/*
Released under this MIT License:
		
Copyright (c) 2010 Bart van der Schoor
		
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
		
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
		
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
	
 */
package bruce 
{
	/**
	 *
	 *	CallQueue
	 *	
	 *	 execute a series of closures, with integrated async through IAsyncCall (serial or paralel)
	 *	 
	 *	 supports nesting in/of other CallQueue's
	 *	 
	 *	part of the Bruce Simple AS3 Banner Toolkit
	 *
	 *	status: looks ready
	 *		
	 * @author Bart van der Schoor @ GML
	 * @version 0.6
	 */
	public class CallQueue implements IAsyncCall
	{		private var _queue:Array = new Array();
		private var _onComplete:Function;
		
		private var _paralel:Boolean = false;		private var _running:Array = new Array();
		
		private static const TYPE:String = 'CallQueue.';

		public function CallQueue(paralel:Boolean=false) 
		{
			_paralel = paralel;
		}

		public function execute(onComplete:Function):void
		{
			_onComplete = onComplete;
			this.next();
		}

		public function add(call:Function, ...args):IAsyncCall
		{
			if (!call)
			{
				Bruce.arg(TYPE + 'add() call' + Bruce.eNull);
				return this;
			}
			
			_queue.push(new Call().init(call, args));
			return this;
		}
		
		public function addAsync(call:IAsyncCall):IAsyncCall
		{
			if (!call)
			{
				Bruce.arg(TYPE + 'addAsync() call' + Bruce.eNull);
				return this;
			}
			_queue.push(call);
			return this;
		}
		
		public function addQueue(call:CallQueue):IAsyncCall
		{
			if (!call)
			{
				Bruce.arg(TYPE + 'addQueue() call' + Bruce.eNull);
				return;
			}
			_queue.push(call);
			return this;
		}

		private function next():void
		{
			if(_running.length > 0)
			{
				return;
			}
			
			while(_queue.length > 0)
			{
				var obj:Object = _queue.shift();
				if(obj is Call)
				{
					//keep stack depth low by calling in the loop
					Call(obj).execute();
				}
				else
				{
					if(_paralel)
					{
						_running.push(obj);
						IAsyncCall(obj).execute(new Call(this.onConcurrent , obj).execute);
					}
					else
					{
						IAsyncCall(obj).execute(this.next);
						return;
					}
				}
			}
						
			if(_onComplete != null)
			{
				//juggle for reset stack-fun
				var f:Function = _onComplete;
				_onComplete = null;
				f.apply();
			}
		}
		
		private function onConcurrent(async:IAsyncCall):void
		{
			var ind:int = _running.indexOf(async);
			if(ind > -1)
			{
				_running.splice(ind, 1);
			}
			this.next();
		}
			
		public function toString():String 
		{
			return '[CallQueue:' + this._queue.length + ':' + this._queue+']';
		}
	}
}